package pzl.com;

import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements ChartView3.IonScroll, ChartView3.IonChartTouch {
    private static final String TAG = "MainActivity";
    private ChartView chartView;
    private ChartView3 chartView3;
    private ChartView4 chartView4;
    private ArrayList<PointF> data;
    private TextView tv_frontIndex, tv_behindIndex;

    private void init() {
        // chartView = (ChartView) findViewById(R.id.cv);
        chartView3 = (ChartView3) findViewById(R.id.cv);
        //chartView4 = (ChartView4) findViewById(R.id.cv);
        tv_frontIndex = (TextView) findViewById(R.id.tv_frontIndex);
        tv_behindIndex = (TextView) findViewById(R.id.tv_behindIndex);
        chartView3.setIonScroll(this);
        chartView3.setIonChartTouch(this);
        data = new ArrayList<PointF>();
        Random random = new Random();
        for (int i = 0; i < 50000; i++) {
            PointF pointF = new PointF();
            pointF.x = i;
            pointF.y = random.nextInt(100);
            data.add(pointF);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
        chartView3.setData(data);
    }


    @Override
    public void onScroll(int frontIndex, int behindIndex, float offsetRate) {
        // Log.i(TAG, "frontIndex=" + frontIndex + ",behindIndex=" + behindIndex + ",offsetRate=" + offsetRate);
        float firstTag = ((PointF) data.get(frontIndex)).x;
        tv_frontIndex.setText(Float.toString(firstTag + offsetRate * 1));
        float behindTag = ((PointF) data.get(behindIndex)).x;
        tv_behindIndex.setText(Float.toString(behindTag + offsetRate * 1));
    }

    @Override
    public void onChartTouch(Object chartData) {
        Toast.makeText(this, "x=" + ((PointF) chartData).x + ",y=" + ((PointF) chartData).y, Toast.LENGTH_LONG).show();
    }
}
